package com.siyuanren.gcocp.common.util;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.siyuanren.gcocp.common.config.PublicConfig;
import com.siyuanren.gcocp.common.consts.BaseConsts;
import com.siyuanren.gcocp.common.web.vo.BaseVO;
import com.siyuanren.gcocp.consts.GcocpConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.Date;
import java.util.Random;

/**
 * Created by   guwr
 * Project_name education-api-parent
 * Path         com.siyuanren.gcocp.common.util.CommonUtils
 * Date         2017/5/16
 * Time         9:38
 * Description
 */
@Slf4j
public class CommonUtils {

    public static String[] weekArr = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    public static String getWeekDay(Date date) {
        boolean sameDay = DateUtils.isSameDay(date, DateUtil.date());
        if (sameDay) {
            return "今天";
        }
        int week = DateUtil.dayOfWeek(date);
        return CommonUtils.weekArr[(week - 1)];
    }

    /**
     * 六位随机数字
     *
     * @return
     */
    public static String getMobileCode() {
        Random rand = new Random();
        int result = rand.nextInt(999999);
        // 对0开头的数字进行处理
        return "000000".substring((result + "").length()) + result;
    }

    /**
     * 手机号码中间区域替换成*
     *
     * @return
     */
    public static String getMobileReplace(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return StringUtils.EMPTY;
        }
        return mobile.replaceAll("(?<=\\d{3})\\d(?=\\d{3})", "*");
    }


    public static void printObj2Json(Object obj, Logger log) {
//        String simpleName = log.getClass().getSuperclass().getSimpleName();
        if (obj == null) {
            log.info("{} is null", "obj");
            return;
        }
        if (obj instanceof String) {
            log.info("{}", obj);
            return;
        }
        log.info("{}", obj2Json(obj));
    }

    public static void printObj2Json(Object obj) {
        printObj2Json(obj, log);
    }

    /**
     * 将对象转换为JSON
     *
     * @param obj
     * @return
     */
    public static String obj2Json(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
    }

    /**
     * 忽略属性
     *
     * @param obj
     * @return
     */
    public static String simpleObj2Json(Object obj) {
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); // 构造方法里，也可以直接传需要序列化的属性名字
        if (obj instanceof BaseVO) {
            filter.getExcludes().add("pageNum");
            filter.getExcludes().add("numPerPage");
            filter.getExcludes().add("sortType");
            filter.getExcludes().add("uuid");
            return JSON.toJSONString(obj, filter, SerializerFeature.WriteMapNullValue);
        }
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
    }


    /*
     * Java文件操作 获取文件扩展名
     *
     *  Created on: 2011-8-2
     *      Author: blueeagle
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /*
     * Java文件操作 获取不带扩展名的文件名
     *
     *  Created on: 2011-8-2
     *      Author: blueeagle
     */
    public static String getFileNameNoEx(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length()))) {
                return filename.substring(0, dot);
            }
        }
        return filename;
    }

    public static void main(String[] args) {
        String extensionName = getExtensionName("aaa.jpg");
        String fileNameNoEx = getFileNameNoEx("aaa.jpg");
        System.out.println("extensionName = " + extensionName);
        System.out.println("fileNameNoEx = " + fileNameNoEx);
        System.out.println("getMobileCode = " + getMobileReplace("15111112222"));
    }

    /**
     * @param address
     * @return
     */
    public static String hostAddress(String address) {
        if (StringUtils.isBlank(address)) {
            return "";
        }
        if (address.startsWith(BaseConsts.HTTP)) {
            return address;
        }
        return PublicConfig.UPLOAD_HOST + address;
    }

}
